<template>
  <a-modal v-model='visible' title='编辑关键字' @cancel='cancel' footer=''>
    <!-- 搜索条件 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline'>
        <a-row :gutter='10'>
          <a-col :md='12' :sm='24'>
            <a-form-item label='关键字'>
              <a-input v-model:value='queryData.keyword' placeholder='请输入关键字名称' />
            </a-form-item>
          </a-col>

          <a-col :md='12' :sm='24'>
            <span class='table-page-search-submitButtons'>
              <a-button type='primary' icon='search' @click='loadData'>查询</a-button>
              <a-button type='primary' style='margin-left: 8px' icon='reload' @click='reload'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button @click='handleAdd' type='primary' icon='plus'>新增</a-button>
      <a-button @click='handleAddByExcel' type='primary' icon='import'>导入</a-button>
      <a-button @click='handleDelBulk' type='primary' icon='delete' :disabled="this.selectedRowKeys.length<=0">删除</a-button>
    </div>

    <a-table
      ref='table'
      size='small'
      bordered
      :scroll={y:500}
      :rowKey='record => record.id'
      childrenColumnName='childrenColumnName'
      :columns='columns'
      :dataSource='dataSource'
      :pagination='ipagination'
      :row-selection="rowSelection"
      :loading='loading'
      @change='onChange'
    >

      <template
        slot='priority'
        slot-scope='text,record,index'
      >
        <a-input style='width:100%' v-model='record.priority' @focus='handleInputFocus(record.priority)'
                 @blur='handleInputConfirm(record,index)' @keyup.enter='handleInputConfirm(record,index)' />
      </template>

      <span slot='action' slot-scope='text, record,index'>
            <a @click='handleDel(record.id)'> 删除 </a>
      </span>
    </a-table>
    <category-item-edit-add-modal @update='loadData' ref='addModal'></category-item-edit-add-modal>
    <upload-category-key-excel ref='upload' @update='loadData'></upload-category-key-excel>
  </a-modal>
</template>

<script>
import ReportsEpidemicDailyService from '@/myService/ReportsEpidemicDailyService'
import CategoryItemEditAddModal
  from '@views/data-governance/smart-reports/epidemic-daily/components/CategoryItemEditAddModal'
import UploadCategoryKeyExcel
  from '@views/data-governance/smart-reports/epidemic-daily/components/UploadCategoryKeyExcel'

export default {
  name: 'CategoryItemEditModel',
  components:{
    CategoryItemEditAddModal,
    UploadCategoryKeyExcel
  },
  data() {
    return {
      visible: false,
      priority: '',
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: (text, row, index) =>
            <span>{this.ipagination.pageSize * (this.ipagination.current - 1) + index + 1}</span>
        },
        {
          title: '关键字',
          dataIndex: 'name',
          align: 'center'
        },
        {
          title: '优先级',
          align: 'center',
          key: 'priority',
          ellipsis: true,
          width: 100,
          scopedSlots: { customRender: 'priority' }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      queryData: {
        keyword: '',
        categoryId: '',
        pageIndex: 1,
        pageSize: 10
      },
      loading: false,
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      dataSource: [],
      selectedRowKeys:[],
      selectedRows:[],
    }
  },
  computed: {
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys
          this.selectedRows = selectedRows;
        },
        selectedRowKeys: this.selectedRowKeys,
      };
    },
  },
  methods: {
    open(categoryId) {
      this.queryData = {
        keyword: '',
        categoryId: categoryId,
        pageIndex: 1,
        pageSize: 10
      }
      this.ipagination.pageSize=10;
      this.ipagination.current=1;
      this.loadData()
      this.visible = true
    },
    loadData() {
      this.loading = true
      this.dataSource = []
      this.queryData.pageSize = this.ipagination.pageSize
      this.queryData.pageIndex = this.ipagination.current
      this.ipagination.total = 0
      ReportsEpidemicDailyService.getReportsEpidemicCategoryKey(this.queryData).then(res => {
        if (res.success) {
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    reload() {
      this.queryData.keyword = '';
      this.loadData()
    },
    onChange(pagination) {
      this.ipagination.current = pagination.current
      this.ipagination.pageSize = pagination.pageSize
      this.loadData()
    },
    cancel() {
      this.$emit('updateCategory')
      this.visible = false
    },
    handleInputFocus(priority) {
      this.priority = priority
    },
    handleInputConfirm(val, index) {
      if (this.priority === val.priority) {
        return
      }
      if (this.isNaturalNum(val.priority)) {
        let params = {
          id: val.id,
          priority: val.priority
        }
        ReportsEpidemicDailyService.editCategoryKeyPriority(params).then(res => {
          if (res.success === true) {
            this.$message.success('更新成功')
            this.loadData()
          }
        }).catch(err => {
          this.$message.error('更新失败')
        })
      } else {
        this.dataSource[index].priority = this.priority
      }
    },
    isNaturalNum(num) {
      if (/^[0-9]+$/.test(num) && (num >= 0)) {
        return true
      } else {
        this.$message.error("非自然数")
        return false
      }
    },
    handleDel(id) {
      ReportsEpidemicDailyService.delCategoryKeyStatus({ id: id }).then(res => {
        if (res.success === true) {
          this.$message.success('删除成功')
          this.loadData()
        }
      })
    },
    handleDelBulk(){
      console.log(this.selectedRowKeys)
      ReportsEpidemicDailyService.delCategoryKeyBulk(this.selectedRowKeys).then(res=>{
        if(res.success){
          this.$message.success("删除成功");
          this.selectedRowKeys=[];
          this.selectedRows=[];
          this.loadData()
        }else {
          this.$message.error(res.message)
        }
      })
    },
    handleAdd() {
      this.$refs.addModal.open(this.queryData.categoryId)
    },
    handleAddByExcel(){
      this.$refs.upload.open(this.queryData.categoryId)
    }
  }
}
</script>

<style scoped>

</style>